package com.xinke.edu.mypetadoption.Activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xinke.edu.mypetadoption.databinding.ActivityChangePasswordBinding;
import com.xinke.edu.mypetadoption.entity.User;
import com.xinke.edu.mypetadoption.net.RetrofitUtil;
import com.xinke.edu.mypetadoption.util.SPUtils;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ChangePassword extends AppCompatActivity {

    private ActivityChangePasswordBinding binding;
    private int userId; // 存储从Intent中接收的用户ID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChangePasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        
        // 从Intent中接收用户ID，直接获取为int类型
        userId = getIntent().getIntExtra("userId", 0);

        // 返回按钮点击事件
        binding.exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 确认修改按钮点击事件
        binding.confirmChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });
    }

    /**
     * 修改密码方法
     */
    private void changePassword() {
        String currentPassword = binding.currentPassword.getText().toString();
        String newPassword = binding.newPassword.getText().toString();
        String newPassword2 = binding.newPassword2.getText().toString();

        // 参数验证
        if (TextUtils.isEmpty(currentPassword)) {
            Toast.makeText(this, "当前密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(newPassword)) {
            Toast.makeText(this, "新密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(newPassword2)) {
            Toast.makeText(this, "确认新密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (newPassword.length() < 6 || newPassword.length() > 16) {
            Toast.makeText(this, "请设置6-16位新密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!newPassword.equals(newPassword2)) {
            Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        // 检查用户ID是否有效
        if (userId == 0) {
            Toast.makeText(this, "用户未登录，请重新登录", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 发送修改密码请求，只传递用户ID和新密码
        RetrofitUtil.getApi().changePassword(userId, newPassword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<com.xinke.edu.mypetadoption.entity.ResultBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull com.xinke.edu.mypetadoption.entity.ResultBody resultBody) {
                        if (resultBody.getCode() == 200) {
                            Toast.makeText(ChangePassword.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(ChangePassword.this, resultBody.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(ChangePassword.this, "网络错误：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放Binding资源，避免内存泄漏
        binding = null;
    }
}